package com.srdl.accountRecord.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.srdl.accountRecord.pojo.AccountRecord;
import com.srdl.accountRecord.mapper.AccountRecordMapper;
import com.srdl.accountRecord.service.AccountRecordService;
import com.utils.common.ResponseEnum;
import com.utils.common.ServerResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* 描述:
* author: srdl
* date: 2022-01-05
*/

@Service
public class AccountRecordServiceImpl implements AccountRecordService {

    private static Logger logger = LoggerFactory.getLogger(AccountRecordServiceImpl.class);

    @Autowired
    private AccountRecordMapper accountRecordMapper ;


    @Override
    public ServerResponse list(AccountRecord accountRecord) {
        try {
            List<AccountRecord> jsonObject = accountRecordMapper.list(accountRecord);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(jsonObject);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).data(e);
        }
    }

    @Override
    public ServerResponse pageList(AccountRecord accountRecord) {
        Map<String,Object> returnMap=new HashMap<>();
        try {
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageSize(accountRecord.getPageSize());
            pageInfo.setPageNum(accountRecord.getPageNum());
            PageHelper.startPage(pageInfo);
            List<AccountRecord> list = accountRecordMapper.list(accountRecord);
            PageInfo<AccountRecord> jsonObject=new PageInfo<AccountRecord>(list);
            returnMap.put("list",jsonObject.getList());
            returnMap.put("pages",jsonObject.getPages());
            returnMap.put("total",jsonObject.getTotal());
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(returnMap);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).data(e);
        }
    }

    @Override
    public ServerResponse get(String id) {
        try {
            AccountRecord jsonObject = accountRecordMapper.get(id);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(jsonObject);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).data(e);
        }
    }

    @Override
    public ServerResponse addAccountRecord(AccountRecord accountRecord) {
        try {
            accountRecord.setId(UUID.randomUUID().toString());
            accountRecordMapper.addAccountRecord(accountRecord);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.ADD_SUCCESS).data(accountRecord);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).message("添加失败");
        }
    }

    @Override
    public ServerResponse deleteAccountRecord(String id) {
        try {
            accountRecordMapper.deleteAccountRecord(id);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.DELETE_SUCCESS);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).message("删除失败");
        }
    }
    @Override
    public ServerResponse updateAccountRecord(AccountRecord accountRecord) {
        try{
            accountRecordMapper.updateAccountRecord(accountRecord);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.UPDATE_SUCCESS).data(accountRecord);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).message("修改失败");
        }
    }

    @Override
    public ServerResponse getSumYearByUserId(String userId) {
        try{
            List<Map<String, Object>> sumByDateTypeAndUserId = accountRecordMapper.getSumYearByUserId(userId);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(sumByDateTypeAndUserId);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).message("查询失败");
        }
    }

    @Override
    public ServerResponse getSumMonthByUserId(String userId) {
        try{
            List<Map<String, Object>> sumByDateTypeAndUserId = accountRecordMapper.getSumMonthByUserId(userId);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(sumByDateTypeAndUserId);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).message("查询失败");
        }
    }

    @Override
    public ServerResponse getSumDayByUserId(String userId) {
        try{
            List<Map<String, Object>> sumByDateTypeAndUserId = accountRecordMapper.getSumDayByUserId(userId);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(sumByDateTypeAndUserId);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).message("查询失败");
        }
    }

    @Override
    public ServerResponse getThisMonthByUserId(String userId) {
        try{
            List<Map<String, Object>> getThisMonthByUserId = accountRecordMapper.getThisMonthByUserId(userId);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(getThisMonthByUserId);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).message("查询失败");
        }
    }

    @Override
    public ServerResponse getLastMonthByUserId(String userId) {
        try{
            List<Map<String, Object>> getLastMonthByUserId = accountRecordMapper.getLastMonthByUserId(userId);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(getLastMonthByUserId);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).message("查询失败");
        }
    }
}
